package com.yingxin.yxerp.module.activiti.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.activiti.dto.AuditDto;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.commons.id.IdHelper;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yxerp.module.activiti.entity.ActHiTaskInst;
import com.yingxin.yxerp.module.activiti.mapper.ActHiTaskInstMapper;
import com.yingxin.yxerp.module.activiti.mapper.ActivitiBizInfoMapper;
import com.yingxin.yxerp.module.activiti.service.ActivitiProcessService;
import com.yingxin.yxerp.module.activiti.service.IActHiTaskInstService;
import com.yingxin.yxerp.module.organization.entity.User;
import com.yingxin.yxerp.module.organization.service.IUserService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ActHiTaskInstServiceImpl extends ServiceImpl<ActHiTaskInstMapper, ActHiTaskInst> implements IActHiTaskInstService {

    @Autowired
    private TaskService taskService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserSupport userSupport;

    @Autowired
    private ActivitiProcessService activitiProcessService;

    @Autowired
    @SuppressWarnings("all")
    private ActivitiBizInfoMapper activitiBizInfoMapper;

    @Autowired
    private ActHiTaskInstMapper actHiTaskInstMapper;

    /**
     * 添加转办历史记录
     */
    @Override
    public void addActHiTaskInst(AuditDto auditDto) {
        Task task = taskService.createTaskQuery().taskId(auditDto.getTaskId()).singleResult();
        if (task == null) {
            throw new BizException("审批任务不存在，请刷新再试");
        }
        // assignee
        String assigneeId = userSupport.getUserId();
        User assigneeUser = userService.getById(assigneeId);
        User user = userService.getById(auditDto.getUserId());
        ActHiTaskInst actHiTaskInst = new ActHiTaskInst();
        actHiTaskInst.setID_(IdHelper.snowflakeId());
        actHiTaskInst.setPROC_DEF_ID_(task.getProcessDefinitionId());
        actHiTaskInst.setTASK_DEF_KEY_(task.getTaskDefinitionKey());
        actHiTaskInst.setPROC_INST_ID_(task.getProcessInstanceId());
        actHiTaskInst.setEXECUTION_ID_(task.getExecutionId());
        actHiTaskInst.setNAME_(assigneeUser.getStaffName() + " 转 " + user.getStaffName());
        actHiTaskInst.setOWNER_(null);
        actHiTaskInst.setASSIGNEE_(assigneeId);
        Date date = new Date();
        actHiTaskInst.setSTART_TIME_(date);
        actHiTaskInst.setCLAIM_TIME_(null);
        actHiTaskInst.setEND_TIME_(date);
        actHiTaskInst.setPRIORITY_("50");
        actHiTaskInst.setFORM_KEY_(task.getFormKey());
        actHiTaskInstMapper.add(actHiTaskInst);
        // 修改审核历史记录
        updateStartTime(auditDto.getTaskId());
        //taskService.addComment(actHiTaskInst.getID_(),userTask.getProcessInstanceId(),assigneeUser.getStaffName()+" 转 "+user.getStaffName());
    }

    /**
     * 强制转办历史记录
     */
    @Override
    public void addActHiTaskInst2(AuditDto auditDto) {
        Task task = taskService.createTaskQuery().taskId(auditDto.getTaskId()).singleResult();
        if (task == null) {
            throw new BizException("审批任务不存在，请刷新再试");
        }
        // assignee
        String assigneeId = userSupport.getUserId();
       /* User assigneeUser = userService.getById(assigneeId);
        User user = userService.getById(auditDto.getUserId());*/
        ActHiTaskInst actHiTaskInst = new ActHiTaskInst();
        actHiTaskInst.setID_(IdHelper.snowflakeId());
        actHiTaskInst.setPROC_DEF_ID_(task.getProcessDefinitionId());
        actHiTaskInst.setTASK_DEF_KEY_(task.getTaskDefinitionKey());
        actHiTaskInst.setPROC_INST_ID_(task.getProcessInstanceId());
        actHiTaskInst.setEXECUTION_ID_(task.getExecutionId());
        actHiTaskInst.setNAME_(auditDto.getComment());
        actHiTaskInst.setOWNER_(null);
        actHiTaskInst.setASSIGNEE_(assigneeId);
        Date date = new Date();
        actHiTaskInst.setSTART_TIME_(date);
        actHiTaskInst.setCLAIM_TIME_(null);
        actHiTaskInst.setEND_TIME_(date);
        actHiTaskInst.setPRIORITY_("50");
        actHiTaskInst.setFORM_KEY_(task.getFormKey());
        actHiTaskInstMapper.add(actHiTaskInst);
        // 修改审核历史记录
        updateStartTime(auditDto.getTaskId());
        //taskService.addComment(actHiTaskInst.getID_(),userTask.getProcessInstanceId(),assigneeUser.getStaffName()+" 转 "+user.getStaffName());
    }

    public void updateStartTime(String taskId){
        HistoricTaskInstance historicTaskById = activitiProcessService.getHistoricTaskById(taskId);
        ActHiTaskInst actHiTaskInst = new ActHiTaskInst();
        actHiTaskInst.setID_(historicTaskById.getId());
        actHiTaskInst.setSTART_TIME_(new Date());
        actHiTaskInstMapper.updateTime(actHiTaskInst);
    }

}
